package logic;

import java.util.Iterator;
import java.util.List;
/**
 * this class is a representation of a landmine. 
 * @author Vossen en Konijnen group 2
 */

public class Landmine implements Actor 
{
    // Whether the hunter is alive or not.
    private boolean alive;
	// The hunter's field.
    private Field field;
    // The hunter's position in the field.
    private Location location;

	/**
	 * Create a Landmine.  
     * @param field The field currently occupied.
     * @param location The location within the field.
	 */
    public Landmine(Field field, Location location) 
	{
    	alive = true;
        this.field = field;
        setLocation(location);
	}

	@Override
	public void act(List<Actor> newActors)
	{
		setDead();
	}

	 /**
     * Place the Landmine at the new location in the given field.
     * @param newLocation The animal's new location.
     */
	@Override
    public void setLocation(Location newLocation)
    {
        if(location != null) {
            field.clear(location);
        }
        location = newLocation;
        field.place(this, newLocation);
    }

	@Override
	public boolean isActive() 
	{
		return alive;
	}

	@Override
	public Location getLocation() 
	{
		return location;
	}

	@Override
	public Field getField() 
	{
		return field;
		
	}

	/**
	 * Makes that the Landmine explodes.
	 */
	public void setInactive() {
		setDead();
		explode();
       if(location != null) {
            field.clear(location);
            location = null;
            field = null;
        }		
	}
	
	/**
	 * The method wich makes the explosion possible.
	 */
	public void explode()
	{
		Field field = getField();
        List<Location> adjacent = field.adjacentLocations(getLocation());
        Iterator<Location> it = adjacent.iterator();
        while(it.hasNext()) 
        {
            Location where = it.next();
            Object actor = field.getObjectAt(where);
            if(actor !=null && !(actor instanceof Landmine))
            {
            	((Actor)actor).setDead();
            }
            List<Location> adjacent2 = field.adjacentLocations(where);
            Iterator<Location> it2 = adjacent2.iterator();
            while(it2.hasNext()) 
            {
            	Location Eplace = it2.next();
            	Object actor2 = field.getObjectAt(Eplace);
            	if(actor2 !=null && !(actor instanceof Landmine))
                {
                	((Actor)actor2).setDead();
                }
            	List<Location> adjacent3 = field.adjacentLocations(Eplace);
                Iterator<Location> it3 = adjacent3.iterator();
                while(it3.hasNext()) 
                {
                	Location Eplace2 = it3.next();
                	Object actor3 = field.getObjectAt(Eplace);
                	if(actor3 !=null && !(actor instanceof Landmine))
                    {
                    	((Actor)actor3).setDead();
                    }
                	
                	Explosion explosion = new Explosion(field, Eplace2);
                }
            }
        }
	}
	/**
	 * Makes that the landmine can't explode anymore.
	 */
	public void setDead()
	{
		alive=false;
	}

}
